<?php
/**
 * Created by PhpStorm.
 * User: zhuanggit
 * Date: 2019/2/28
 * Time: 14:53
 */
namespace App\Observers;

use App\Models\Task;
use Illuminate\Support\Facades\DB;

class TaskObserver
{
    public function updated(Task $task)
    {
        if ($task->state!=0){
            DB::table('grades')
                ->updateOrInsert(
                    ['program_id' => $task->program_id, 'member_id' =>$task->member_id],
                    ['program_id' =>$task->program_id, 'usetime' => $task->time, 'member_id' => $task->member_id,'branch_id'=>$task->branch_id]
                );
            $temp=Task::where('program_id',$task->program_id)->where('state',0)->count();
            if($temp==0){
                DB::table('programs')->where('id', $task->program_id)->update(['plan'=>'B']);
            }
        }
    }
}
